package com.ygsoft.tianlu_mall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygsoft.tianlu_mall.pojo.Msg;
import com.ygsoft.tianlu_mall.pojo.Search;
import com.ygsoft.tianlu_mall.service.NumService;
import com.ygsoft.tianlu_mall.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SearchController {

    @Autowired
    SearchService searchService;

    @Autowired
    NumService numService;

    //获得热词
    @RequestMapping(value = "/getHotword",method = RequestMethod.GET)
    @ResponseBody
    public Msg getHotword(){
        try {
            List<String> hotword= searchService.getTop5Hotword();
            return Msg.succes().add("hotword",hotword);
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //检查热词是否存在数据库 存在 返回true，不存在返回false
    public boolean checkHotwordIsExist(String search_content){
        Boolean isExist = true;
        List<String> search_content_list = searchService.checkHotwordIsExist();
        if (search_content_list.contains(search_content))
            return isExist;
        else
            isExist = false;
        return isExist;

    }

    public Msg updateHotword(@RequestParam("search_content") String search_content){
        try {
            if (!checkHotwordIsExist(search_content)){
                searchService.insertHotword(search_content);
                return Msg.succes();
            }else {
                //存在则修改查询次数
                searchService.updateHotwordCount(search_content);
                return Msg.succes();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //主页模糊查询商品
    @RequestMapping(value = "/searchGoods",method = RequestMethod.GET)
    @ResponseBody
    public Msg searchGoods(@RequestParam("search_content") String search_content,
                           @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                           @RequestParam Integer pageSize){
        try {
            if (search_content.length()!=0){
                updateHotword(search_content);
                PageHelper.startPage(pageNum,pageSize);
                List<Map<String,Object>> goodslist = searchService.getGoodsDetailByLike(search_content);
                PageInfo goodsList = new PageInfo(goodslist,5);
                Map<Object,Object> result = new HashMap<>();
                List<HashMap<String,Object>> goodsNum = new ArrayList<>();
                List goods_idList = searchService.getGoodsIdBySearchContent(search_content);
                for (Object goods_id:goods_idList){
                    HashMap<String,Object> goodsNumMap =numService.getThreeNum((int)goods_id);
                    goodsNum.add(goodsNumMap);
                }
                if (goodslist.size()==0){
                    String message = "查询结果为空,请更换关键词！";
                    return Msg.fail().add("message",message);
                }else {
                    result.put("goodsList",goodsList);
                    result.put("goodsNum",goodsNum);
                    return Msg.succes().add("result",result);
                }
            }else {
                String message = "请输入关键词";
                return Msg.fail().add("message",message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }

    //店铺模糊查询商品
    @RequestMapping(value = "/searchGoodsInShop",method = RequestMethod.GET)
    @ResponseBody
    public Msg searchGoodsInShop(@RequestParam int shop_id,@RequestParam("search_content") String search_content){
        try {
            if (search_content.length()!=0){

                updateHotword(search_content);
                List<Map<String,Object>> goodsList = searchService.getGoodsDetailByLikeInShop(shop_id,search_content);
                Map<Object,Object> result = new HashMap<>();
                List<HashMap<String,Object>> goodsNum = new ArrayList<>();
                List goods_idList = searchService.getGoodsIdByShopIdAndSearchContent(shop_id,search_content);
                for (Object goods_id:goods_idList){
                    HashMap<String,Object> goodsNumMap =numService.getThreeNum((int)goods_id);
                    goodsNum.add(goodsNumMap);
                }
                if (goodsList.size()==0){
                    String message = "查询结果为空,请更换关键词！";
                    return Msg.fail().add("message",message);
                }else {
                    result.put("goodsList",goodsList);
                    result.put("goodsNum",goodsNum);
                    return Msg.succes().add("result",result);
                }
            }else {
                String message = "请输入关键词";
                return Msg.fail().add("message",message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Msg.fail();
        }
    }
}
